package com.bky.queue;

import com.bky.service.impl.QueryService;
import org.slf4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.concurrent.BlockingQueue;

/**
 * Created by shipeilei on 2017/5/27.
 */
public class Produce implements Runnable{
    private QueryService queryService;
    private BlockingQueue queue;
    private Logger LOGGER;

    public Produce(BlockingQueue queue, QueryService queryService, Logger LOGGER) {
        this.queue = queue;
        this.queryService = queryService;
        this.LOGGER = LOGGER;
    }

    @Override
    public void run() {
        LOGGER.info("队列生产者，当前线程编号：{}, 线程名称：{}", Thread.currentThread().getId(), Thread.currentThread().getName());
        queryService.addObserver(new Observer() {
            @Override
            public void update(Observable o, Object arg) {
                List<Map<String, Object>> mapList = (List<Map<String, Object>>) arg;
                for (int i=0; i<mapList.size(); i++){
                    Map map = mapList.get(i);
                    try {
                        queue.put(map);
                    } catch (InterruptedException e) {
                        LOGGER.error("队列信息插入异常", e);
                    }
                }
            }
        });
        queryService.startQuery(0, 5);
    }
}
